package spring.chatroom.java_chatroom.common.utils;

import cn.hutool.crypto.digest.DigestUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.UUID;


@Slf4j
public class SecurityUtil {

    public static String encrypt(String password) {
        //每次都获得一个随机的盐 长度是固定的32位
        String salt = UUID.randomUUID().toString().replace("-", "");

        System.out.println("salt=" + salt + " saltLength:" + salt.length());

        //将这俩个合在一起加密
        String finalPassword = DigestUtil.md5Hex(salt + password);

        System.out.println("最终的密文" + finalPassword);

        return salt + finalPassword;
    }

    public static boolean verify(String verifyPassword, String passwordDB) {
        String salt = passwordDB.substring(0, 32);
        System.out.println("verify salt = " + salt);

        //使用同一个加密方式加密
        String md5Password = DigestUtil.md5Hex(salt + verifyPassword);
        System.out.println("verify = " + md5Password);

        return (salt+md5Password).equals(passwordDB);
    }
}
